﻿#include"../include/MyselfWord.h"
#include"include/MyChild.h"
#include<qapplication.h>
#include<qmdiarea.h>
#include<qmenubar.h>
#include<qfont.h>
#include<qactiongroup.h>
#include<qpixmap.h>
#include<qmessagebox.h>
#include<qtoolbar.h>
#include<qcombobox.h>
#include<qfontcombobox.h>
#include<qfontdatabase.h>
#include<QMdiSubWindow>
#include<qsignalmapper.h>
#include<QCloseEvent>
#include<QStatusBar>
#include<qfiledialog.h>
#include<qcolordialog.h>
#include<QPrintDialog>
#include<QPrinter>
#include<QPrintPreviewDialog>

MyWord::MyWord(QWidget* parent):QMainWindow(parent)
{
	mdiArea = new QMdiArea;
	mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	this->setCentralWidget(mdiArea);
	connect(mdiArea, &QMdiArea::subWindowActivated, this, &MyWord::updateMenus);
	windowMapper = new QSignalMapper(this);
	//映射器重新发送信号，根据信号设置活动窗口
	//connect(windowMapper, &QSignalMapper::mappedObject, this, &MyWord::setActiveSubWindow);

	this->createActions();
	this->createMenu();
	this->createToolBars();
	this->createStatusBar();
	this->updateMenus();

	this->move(200, 150);
	this->resize(800, 500);
	this->setWindowTitle(tr("mySelf Word"));
}

MyWord::~MyWord()
{
}

void MyWord::closeEvent(QCloseEvent* event) {
	mdiArea->closeAllSubWindows();
	if (mdiArea->currentSubWindow())
		event->ignore();
	else
		event->accept();
}

void MyWord::updateMenus() {
	//至少有一个子文档打开的情况
	bool hasMyChild = (this->activeMyChild() != 0);
	saveAct->setEnabled(hasMyChild);
	saveAsAct->setEnabled(hasMyChild);
	printAct->setEnabled(hasMyChild);
	printPreviewAct->setEnabled(hasMyChild);
	pasteAct->setEnabled(hasMyChild);
	closeAct->setEnabled(hasMyChild);
	closeAllAct->setEnabled(hasMyChild);
	tileAct->setEnabled(hasMyChild);
	cascadeAct->setEnabled(hasMyChild);
	nextAct->setEnabled(hasMyChild);
	previousAct->setEnabled(hasMyChild);

}

void MyWord::fileNew() {
	MyChild* child = this->createMyChild();
	child->newFile();
	child->show();
	this->enabledText();
}

void MyWord::fileOpen()
{
	QString fileName = QFileDialog::getOpenFileName(this, tr("打开"), QString(),
		tr("HTML 文档 (*.htm *.html);;所有文件(*.*)"));
	if (!fileName.isEmpty()) {
		QMdiSubWindow* existing = this->findMyChild(fileName);
		if (existing) {
			mdiArea->setActiveSubWindow(existing);
			return;
		}
		MyChild* child = this->createMyChild();
		if (child->loadFile(fileName)) {
			this->statusBar()->showMessage(tr("文件已载入"), 2000);
			child->show();
		}
		else
			child->close();
	}
}

void MyWord::fileSave()
{
	if (this->activeMyChild() && this->activeMyChild()->save())
		this->statusBar()->showMessage(tr("保存成功"), 2000);
}

void MyWord::fileSaveAs()
{
	if (this->activeMyChild() && this->activeMyChild()->saveAs())
		this->statusBar()->showMessage(tr("保存成功"), 2000);
}

void MyWord::filePrint()
{
	QPrinter printer(QPrinter::HighResolution);
	QPrintDialog* dlg = new QPrintDialog(&printer, this);
	if (this->activeMyChild()->textCursor().hasSelection())
		dlg->setOption(QPrintDialog::PrintSelection);
	dlg->setWindowTitle(tr("打印文档"));
	if (dlg->exec() == QDialog::Accepted)
		this->activeMyChild()->print(&printer);
	delete dlg;
}

void MyWord::filePrintPreview()
{
	QPrinter printer(QPrinter::HighResolution);
	QPrintPreviewDialog preview(&printer, this);
	connect(&preview, &QPrintPreviewDialog::paintRequested, this, &MyWord::printPreview);
	preview.exec();
}

void MyWord::printPreview(QPrinter* printer)
{
	this->activeMyChild()->print(printer);
}

void MyWord::undo()
{
	if (this->activeMyChild())
		this->activeMyChild()->undo();
}

void MyWord::redo()
{
	if (this->activeMyChild())
		this->activeMyChild()->redo();
}

void MyWord::cut()
{
	if (this->activeMyChild())
		this->activeMyChild()->cut();
}

void MyWord::copy()
{
	if (this->activeMyChild())
		this->activeMyChild()->copy();
}

void MyWord::paste()
{
	if (this->activeMyChild())
		this->activeMyChild()->paste();
}

void MyWord::textBold()
{
	QTextCharFormat fmt;
	fmt.setFontWeight(boldAct->isChecked() ? QFont::Bold : QFont::Normal);
	if (this->activeMyChild())
		this->activeMyChild()->mergeFormatOnSelection(fmt);
}

void MyWord::textItalic()
{
	QTextCharFormat fmt;
	fmt.setFontItalic(italiAct->isChecked());
	if (this->activeMyChild())
		this->activeMyChild()->mergeFormatOnSelection(fmt);
}

void MyWord::textUnderLine()
{
	QTextCharFormat fmt;
	fmt.setFontUnderline(underLineAct->isChecked());
	if (this->activeMyChild())
		this->activeMyChild()->mergeFormatOnSelection(fmt);
}

void MyWord::textStyle(int styleIndex)
{
	if (this->activeMyChild())
		this->activeMyChild()->setStyle(styleIndex);
}

void MyWord::textFamily(const QString& f)
{
	QTextCharFormat fmt;
	fmt.setFontFamily(f);
	if (this->activeMyChild())
		this->activeMyChild()->mergeFormatOnSelection(fmt);
}

void MyWord::textSize(const QString& p)
{
	double pointSize = p.toDouble();
	if (pointSize > 0) {
		QTextCharFormat fmt;
		fmt.setFontPointSize(pointSize);
		if (this->activeMyChild())
			this->activeMyChild()->mergeFormatOnSelection(fmt);
	}
}


void MyWord::textAlign(const QAction* a)
{
	if (a == leftAlignAct && this->activeMyChild() != nullptr)
		this->activeMyChild()->setAlign(1);
	else if (a == centerAct && this->activeMyChild() != nullptr)
		this->activeMyChild()->setAlign(2);
	else if (a == rightAlignAct && this->activeMyChild() != nullptr)
		this->activeMyChild()->setAlign(3);
	else if (a == justifyAct && this->activeMyChild() != nullptr)
		this->activeMyChild()->setAlign(4);
}

void MyWord::textColor()
{
	if (this->activeMyChild()) {
		QColor color = QColorDialog::getColor(this->activeMyChild()->textColor(), this);
		if (!color.isValid())
			return;
		QTextCharFormat fmt;
		fmt.setForeground(color);
		this->activeMyChild()->mergeFormatOnSelection(fmt);
		
	}
}

void MyWord::about() {
	QMessageBox::about(this, tr("关于"), tr("这是一个基于Qt实现的文字处理软件，具备类似微软Office word的功能。"));
}

MyChild* MyWord::createMyChild()
{
	MyChild* child = new MyChild;
	mdiArea->addSubWindow(child);	//向多文档区域添加子窗口，child为中心部件
	//根据QtextEdit类是否可以复制信号，设置剪切，复制动作是否可用
	connect(child, &MyChild::copyAvailable, cutAct, &QAction::setEnabled);
	connect(child, &MyChild::copyAvailable, copyAct, &QAction::setEnabled);
	return child;
}

void MyWord::updateWindowMenu()
{
	//首先清空菜单，然后再添加各个菜单动作
	windowMenu->clear();
	windowMenu->addAction(closeAct);
	windowMenu->addAction(closeAllAct);
	windowMenu->addSeparator();
	windowMenu->addAction(tileAct);
	windowMenu->addAction(cascadeAct);
	windowMenu->addSeparator();
	windowMenu->addAction(nextAct);
	windowMenu->addAction(previousAct);
	windowMenu->addAction(separatorAct);
	//如果有活动窗口，则显示分隔条
	QList<QMdiSubWindow*> windows = mdiArea->subWindowList();
	separatorAct->setVisible(!windows.isEmpty());
	//遍历各个子窗口，显示当前已打开的文档子窗口项
	for (int i = 0; i < windows.size(); ++i) {
		MyChild* child = qobject_cast<MyChild*>(windows.at(i)->widget());
		QString text;
		if (i < 9)
			text = tr("&%1 %2").arg(i + 1).arg(child->userFriendlyCurrentFile());
		else
			text = tr("&%1 %2").arg(i + 1).arg(child->userFriendlyCurrentFile());
		//添加动作到菜单，设置动作可以选择
		QAction* action = windowMenu->addAction(text);
		action->setCheckable(true);
		//设置当前活动窗口动作为选中状态
		action->setChecked(child == this->activeMyChild());
		//关联动作的触发信号到信号映射器的map()槽，这个槽会发送mapped()信号
		bool b = connect(action, &QAction::triggered, windowMapper, [=] {this->windowMapper->map(); });
		//bool b=connect(action, SIGNAL(triggerd()),windowMapper,SLOT(map()));
		qDebug() << b;
		//将动作与相应的窗口部件进行映射，在发送mapped()信号时就会以这个窗口部件为参数
		windowMapper->setMapping(action, windows.at(i));
	}
	connect(windowMapper, &QSignalMapper::mappedObject, mdiArea, [=](QObject* object) {
		auto subWindow = qobject_cast<QMdiSubWindow*>(object);
		mdiArea->setActiveSubWindow(subWindow);
		});
	this->enabledText();
}

void MyWord::createMenu()
{
	//"文件"主菜单
	fileMenu = this->menuBar()->addMenu(tr("文件(&F)"));
	fileMenu->addAction(newAct);
	fileMenu->addAction(openAct);
	fileMenu->addSeparator();	//分割线
	fileMenu->addAction(saveAct);
	fileMenu->addAction(saveAsAct);
	fileMenu->addSeparator();	//分割线
	fileMenu->addAction(printAct);
	fileMenu->addAction(printPreviewAct);
	fileMenu->addSeparator();
	fileMenu->addAction(exitAct);

	//"编辑"主菜单
	editMenu = this->menuBar()->addMenu(tr("编辑(&E)"));
	editMenu->addAction(undoAct);
	editMenu->addAction(redoAct);
	editMenu->addSeparator();
	editMenu->addAction(cutAct);
	editMenu->addAction(copyAct);
	editMenu->addAction(pasteAct);

	//"格式"主菜单
	formatMenu = this->menuBar()->addMenu(tr("格式(&O)"));
	fontMenu = formatMenu->addMenu(tr("字体(&D)"));	//“字体”子菜单
	fontMenu->addAction(boldAct);
	fontMenu->addAction(italiAct);
	fontMenu->addAction(underLineAct);
	alignMenu = formatMenu->addMenu(tr("段落"));
	alignMenu->addAction(leftAlignAct);
	alignMenu->addAction(centerAct);
	alignMenu->addAction(rightAlignAct);
	alignMenu->addAction(justifyAct);
	formatMenu->addAction(colorAct);

	//"窗口"主菜单
	windowMenu = this->menuBar()->addMenu(tr("窗口(&W)"));
	this->updateWindowMenu();
	connect(windowMenu, &QMenu::aboutToShow, this, &MyWord::updateWindowMenu);
	this->menuBar()->addSeparator();

	//"帮助"主菜单
	helpMenu = this->menuBar()->addMenu(tr("帮助(&H)"));
	helpMenu->addAction(aboutAct);
	helpMenu->addSeparator();
	helpMenu->addAction(aboutQtAct);
}

void MyWord::createToolBars()
{
	//"文件"工具条
	fileToolBar = this->addToolBar(tr("文件"));
	fileToolBar->addAction(newAct);
	fileToolBar->addAction(openAct);
	fileToolBar->addAction(saveAct);
	fileToolBar->addSeparator();
	fileToolBar->addAction(printAct);
	//"编辑"工具条
	editToolBar = this->addToolBar(tr("编辑"));
	editToolBar->addAction(undoAct);
	editToolBar->addAction(redoAct);
	editToolBar->addSeparator();
	editToolBar->addAction(cutAct);
	editToolBar->addAction(copyAct);
	editToolBar->addAction(pasteAct);
	//"格式"工具条
	formatToolBar = this->addToolBar(tr("格式"));
	formatToolBar->addAction(boldAct);
	formatToolBar->addAction(italiAct);
	formatToolBar->addAction(underLineAct);
	formatToolBar->addSeparator();
	formatToolBar->addAction(leftAlignAct);
	formatToolBar->addAction(centerAct);
	formatToolBar->addAction(rightAlignAct);
	formatToolBar->addAction(justifyAct);
	formatToolBar->addSeparator();
	formatToolBar->addAction(colorAct);
	//组合选择栏
	this->addToolBarBreak(Qt::TopToolBarArea);	//使工具栏分多行显示
	comboToolBar = this->addToolBar(tr("组合选择"));
	comboStyle = new QComboBox();
	comboToolBar->addWidget(comboStyle);
	comboStyle->addItem("标准");
	comboStyle->addItem("项目符号（●）");
	comboStyle->addItem("项目符号（○）");
	comboStyle->addItem("项目符号（■）");
	comboStyle->addItem("编号（1.2.3）");
	comboStyle->addItem("编号（a.b.c）");
	comboStyle->addItem("编号（A.B.C）");
	comboStyle->addItem("编号（i.ii.iii）");
	comboStyle->addItem("编号（I.II.III）");
	comboStyle->setStatusTip("段落加标号或编号");
	connect(comboStyle, &QComboBox::activated, this, &MyWord::textStyle);
	comboFont = new QFontComboBox();
	comboToolBar->addWidget(comboFont);
	comboFont->setStatusTip("更改字体");
	connect(comboFont, &QFontComboBox::textActivated, this, &MyWord::textFamily);
	comboSize = new QComboBox();
	comboToolBar->addWidget(comboSize);
	comboSize->setEditable(true);
	comboSize->setStatusTip("更改字号");
	QFontDatabase db;
	foreach(int size, db.standardSizes()) {
		comboSize->addItem(QString::number(size));
		connect(comboSize, &QComboBox::textActivated, this, &MyWord::textSize);
		comboSize->setCurrentIndex(comboSize->findText(QString::number(qApp->font().pointSize())));
	}
}

void MyWord::createStatusBar()
{
	this->statusBar()->showMessage(tr("就绪"));
}

void MyWord::createActions() {
	/*"文件"主菜单动作集*/
	newAct = new QAction(QIcon(":/image/filenew.png"), tr("新建(&N)"), this);
	newAct->setShortcut(QKeySequence::New);
	newAct->setStatusTip(tr("创建一个新文档"));
	connect(newAct, &QAction::triggered, this, &MyWord::fileNew);

	openAct = new QAction(QIcon(":/image/fileopen.png"), tr("打开(&O)"), this);
	openAct->setShortcuts(QKeySequence::Open);
	openAct->setToolTip("打开");
	openAct->setStatusTip(tr("打开已存在的文档"));
	connect(openAct, &QAction::triggered, this, &MyWord::fileOpen);
	
	saveAct = new QAction(QIcon(":/image/filesave.png"), tr("保存"), this);
	saveAct->setShortcuts(QKeySequence::Save);
	saveAct->setToolTip("保存");
	saveAct->setStatusTip(tr("将当前文档存盘"));
	connect(saveAct, &QAction::triggered, this, &MyWord::fileSave);

	saveAsAct = new QAction(tr("另存为(&A)..."), this);
	saveAsAct->setShortcuts(QKeySequence::SaveAs);
	saveAsAct->setStatusTip(tr("以一个新的名字保存文档"));
	connect(saveAsAct, &QAction::triggered, this, &MyWord::fileSaveAs);

	printAct = new QAction(QIcon(":/image/fileprint.png"), tr("打印(&P)"), this);
	printAct->setShortcuts(QKeySequence::Print);
	printAct->setToolTip("打印");
	printAct->setStatusTip(tr("打印文档"));
	connect(printAct, &QAction::triggered, this, &MyWord::filePrint);

	printPreviewAct = new QAction(tr("打印预览..."), this);
	printPreviewAct->setStatusTip(tr("预览打印效果"));
	connect(printPreviewAct, &QAction::triggered, this, &MyWord::filePrintPreview);

	exitAct = new QAction(tr("退出(&X)"), this);
	exitAct->setShortcuts(QKeySequence::Quit);
	exitAct->setStatusTip(tr("退出应用程序"));
	connect(exitAct, &QAction::triggered, qApp, &QApplication::closeAllWindows);

	/******************************************************************************/
	/*“编辑”主菜单动作集*/
	undoAct = new QAction(QIcon(":/image/editundo.png"), tr("撤销"), this);
	undoAct->setShortcut(QKeySequence::Undo);
	undoAct->setToolTip("撤销");
	undoAct->setStatusTip(tr("撤销当前操作"));
	connect(undoAct, &QAction::triggered, this, &MyWord::undo);

	redoAct = new QAction(QIcon(":/image/editredo.png"), tr("重做"), this);
	redoAct->setShortcut(QKeySequence::Redo);
	redoAct->setToolTip("重做");
	redoAct->setStatusTip(tr("恢复之前操作"));
	connect(redoAct, &QAction::triggered, this, &MyWord::redo);

	cutAct = new QAction(QIcon(":image/editcut.png"), tr("剪切(&T)"), this);
	cutAct->setShortcut(QKeySequence::Cut);
	cutAct->setToolTip("剪切");
	cutAct->setStatusTip(tr("剪切所选内容，并将其放入剪贴板"));
	connect(cutAct, &QAction::triggered, this, &MyWord::cut);

	copyAct = new QAction(QIcon(":/image/editcopy.png"), tr("复制(&C)"), this);
	copyAct->setShortcut(QKeySequence::Copy);
	copyAct->setToolTip("复制");
	copyAct->setStatusTip(tr("复制所选内容，并将其放入剪贴板"));
	connect(copyAct, &QAction::triggered, this, &MyWord::copy);

	pasteAct = new QAction(QIcon(":/image/editpaste.png"), tr("粘贴(&P)"), this);
	pasteAct->setShortcut(QKeySequence::Paste);
	pasteAct->setToolTip("粘贴");
	pasteAct->setStatusTip(tr("将剪贴板内容粘贴到文档"));
	connect(pasteAct, &QAction::triggered, this, &MyWord::paste);

	/******************************************************************************/
	/*"格式"主菜单动作集*/
	boldAct = new QAction(QIcon(":/image/textbold.png"), tr("加粗(&B)"), this);
	boldAct->setCheckable(true);
	boldAct->setShortcut(Qt::CTRL + Qt::Key_B);
	boldAct->setToolTip("加粗");
	boldAct->setStatusTip(tr("将所选文字加粗"));
	QFont bold;
	bold.setBold(true);
	boldAct->setFont(bold);
	connect(boldAct, &QAction::triggered, this, &MyWord::textBold);

	italiAct = new QAction(QIcon(":/image/textitalic.png"), tr("倾斜(&I)"), this);
	italiAct->setCheckable(true);
	italiAct->setShortcut(Qt::CTRL + Qt::Key_I);
	italiAct->setToolTip("倾斜");
	italiAct->setStatusTip(tr("将所选文字倾斜"));
	QFont italic;
	italic.setItalic(true);
	italiAct->setFont(italic);
	connect(italiAct, &QAction::triggered, this, &MyWord::textItalic);

	underLineAct = new QAction(QIcon(":/image/textunder.png"), tr("下划线(&U)"), this);
	underLineAct->setCheckable(true);
	underLineAct->setShortcut(Qt::CTRL + Qt::Key_U);
	underLineAct->setToolTip("下划线");
	underLineAct->setStatusTip(tr("将所选文字添加下划线"));
	QFont underline;
	italic.setUnderline(true);
	underLineAct->setFont(underline);
	connect(underLineAct, &QAction::triggered, this, &MyWord::textUnderLine);

	//"格式"->"段落"子菜单下的各项为同一个菜单项组，只能选中其中一项
	QActionGroup* grp = new QActionGroup(this);
	connect(grp, &QActionGroup::triggered, this, &MyWord::textAlign);
	if (QApplication::isLeftToRight()) {
		leftAlignAct = new QAction(QIcon(":/image/textleft.png"), tr("左对齐(&L)"), grp);
		centerAct = new QAction(QIcon(":/image/textcenter.png"), tr("居中(&E)"), grp);
		rightAlignAct = new QAction(QIcon(":/image/textright.png"), tr("右对齐(&R)"), grp);
	}
	else {
		rightAlignAct = new QAction(QIcon(":/image/textright.png"), tr("右对齐(&R)"), grp);
		centerAct = new QAction(QIcon(":/image/textcenter.png"), tr("居中(&E)"), grp);
		leftAlignAct = new QAction(QIcon(":/image/textleft.png"), tr("左对齐(&L)"), grp);
	}
	justifyAct = new QAction(QIcon(":/image/textjustify.png"), tr("两端对齐(&J)"), grp);
	leftAlignAct->setShortcut(Qt::CTRL + Qt::Key_L);
	leftAlignAct->setCheckable(true);
	leftAlignAct->setToolTip("左对齐");
	leftAlignAct->setStatusTip(tr("将文字左对齐"));
	centerAct->setShortcut(Qt::CTRL + Qt::Key_E);
	centerAct->setCheckable(true);
	centerAct->setToolTip("居中");
	centerAct->setStatusTip(tr("将文字居中对齐"));
	rightAlignAct->setShortcut(Qt::CTRL + Qt::Key_R);
	rightAlignAct->setCheckable(true);
	rightAlignAct->setToolTip("右对齐");
	justifyAct->setStatusTip(tr("将文字两端对齐"));
	justifyAct->setShortcut(Qt::CTRL + Qt::Key_J);
	justifyAct->setCheckable(true);
	justifyAct->setToolTip("两端对齐");
	justifyAct->setStatusTip(tr("将文字两端对齐"));

	QPixmap pix(16, 16);
	pix.fill(Qt::red);
	colorAct = new QAction(pix, tr("颜色(&C)..."), this);
	colorAct->setToolTip("颜色");
	colorAct->setStatusTip(tr("设置文字颜色"));
	connect(colorAct, &QAction::triggered, this, &MyWord::textColor);

	/******************************************************************************/
	/*"窗口"主菜单动作集*/
	closeAct = new QAction(tr("关闭(&O)"), this);
	closeAct->setStatusTip(tr("关闭活动子窗口"));
	connect(closeAct, &QAction::triggered, mdiArea, &QMdiArea::closeActiveSubWindow);

	closeAllAct = new QAction(tr("关闭所有(&A)"), this);
	closeAllAct->setStatusTip(tr("关闭所有子窗口"));
	connect(closeAllAct, &QAction::triggered, mdiArea, &QMdiArea::closeAllSubWindows);

	tileAct = new QAction(tr("平铺(&T)"), this);
	tileAct->setStatusTip(tr("平铺子窗口"));
	connect(tileAct, &QAction::triggered, mdiArea, &QMdiArea::tileSubWindows);

	cascadeAct = new QAction(tr("层叠(&C)"), this);
	cascadeAct->setStatusTip(tr("层叠子窗口"));
	connect(cascadeAct, &QAction::triggered, mdiArea, &QMdiArea::cascadeSubWindows);

	nextAct = new QAction(tr("下一个(&C)"), this);
	nextAct->setShortcut(QKeySequence::NextChild);
	nextAct->setStatusTip(tr("移动焦点到下一个子窗口"));
	connect(nextAct, &QAction::triggered, mdiArea, &QMdiArea::activateNextSubWindow);

	previousAct = new QAction(tr("前一个(&V)"), this);
	previousAct->setShortcut(QKeySequence::PreviousChild);
	previousAct->setStatusTip(tr("移动焦点到前一个子窗口"));
	connect(previousAct, &QAction::triggered, mdiArea, &QMdiArea::activatePreviousSubWindow);

	separatorAct = new QAction(this);
	separatorAct->setSeparator(true);

	/******************************************************************************/
	/*"帮助"菜单动作集*/
	aboutAct = new QAction(tr("关于(&A)"), this);
	aboutAct->setStatusTip(tr("关于mySelf Word"));
	connect(aboutAct, &QAction::triggered, this, &MyWord::about);

	aboutQtAct = new QAction(tr("关于Qt(&Q)"), this);
	aboutQtAct->setStatusTip(tr("关于Qt库"));
	connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
}


void MyWord::enabledText()
{
	boldAct->setEnabled(true);
	italiAct->setEnabled(true);
	underLineAct->setEnabled(true);
	leftAlignAct->setEnabled(true);
	centerAct->setEnabled(true);
	rightAlignAct->setEnabled(true);
	justifyAct->setEnabled(true);
	colorAct->setEnabled(true);
}

MyChild* MyWord::activeMyChild()
{
	//若有活动窗口，则将其内的中心部件转换为MyChild类型；若没有，则直接返回null
	if (QMdiSubWindow* activeSubWindow = mdiArea->activeSubWindow())
		return qobject_cast<MyChild*>(activeSubWindow->widget());
	return nullptr;
}

QMdiSubWindow* MyWord::findMyChild(const QString& fileName)
{
	QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
	foreach(QMdiSubWindow * window, mdiArea->subWindowList()) {
		MyChild* myChild = qobject_cast<MyChild*>(window->widget());
		if (myChild->currentFile() == canonicalFilePath)
			return window;
	}
	return nullptr;
}
